package ebedynky.services;

import ebedynky.beans.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * This class tries to find user in database and grant him privileges.
 *
 *
 * @author Jan Remes
 */
public class MyUserDetailService implements UserDetailsService {

    private List<String> roles = new ArrayList<String>();
    private static User loggedUser = null;

    /**
     * Loads the user by username.
     *
     * @param username of the user to be loaded
     * @return UserDetails for the user
     * @throws UsernameNotFoundException when user with such username not in db
     * @throws DataAccessException on data access error
     */
    @Override
    public UserDetails loadUserByUsername(String username) {
        UserService us = new UserService();
        loggedUser = us.getUserByUsername(username);
        this.roles.clear();

        if (loggedUser == null) {
            throw new UsernameNotFoundException("User not found.");
        }

        String role = loggedUser.getRoles();
        String[] rs = new String[0];
        if (role != null) {
            rs = role.split(",");
        }

        for (int i = 0; i < rs.length; i++) {
            String r = rs[i];
            if (r.equals("admin")) {

                this.roles.add("ROLE_ADMIN");

            } else if (r.equals("customer")) {

                this.roles.add("ROLE_CUSTOMER");
                this.roles.add("ROLE_USER");

            } else if (r.equals("employee")) {

                this.roles.add("ROLE_EMPLOYEE");
                this.roles.add("ROLE_USER");

            } else if (r.equals("supplier")) {

                this.roles.add("ROLE_SUPLIER");
                this.roles.add("ROLE_USER");
            }
        }

             org.springframework.security.core.userdetails.User usr =
                new org.springframework.security.core.userdetails.User(
                loggedUser.getUsername(),
                loggedUser.getPass(),
                loggedUser.getAccountState(),
                true,
                true,
                true,
                getAuthorities());
        return usr;
    }
    /**Convert list to Authority list
     *
     * @return Authorities list
     */
    private List<GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();

        for (String role : this.roles) {
            authList.add(new GrantedAuthorityImpl(role));
        }

        return authList;
    }
    /**
     *
     * @return String of how many users are currently logged in
     */
    public static String getLoggedUsernameWithRoles() {
        return loggedUser.getUsername() + " (" + loggedUser.getRoles() + ')';
    }
}
